package com.sky.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.Dao.AddressBookDao;
import com.sky.Service.AddressBookService;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AddressBookServiceImpl
        extends ServiceImpl<AddressBookDao, AddressBook>
        implements AddressBookService {

    @Autowired
    private AddressBookDao addressBookDao;

    @Override
    public List<AddressBook> getAddressBookLists() {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        //根据当前用户id查询地址簿列表
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentUserId);
        List<AddressBook> addressBooks = addressBookDao.selectList(queryWrapper);
        //返回地址簿列表
        return addressBooks;
    }

    @Override
    public void addAddressBook(AddressBook addressBook) {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        //设置当前用户id
        addressBook.setUserId(currentUserId);
        //添加地址信息
        addressBookDao.insert(addressBook);
    }

    @Override
    public void setDefaultAddressBook(Long id) {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentUserId);
        List<AddressBook> addressBooks = addressBookDao.selectList(queryWrapper);
        for (AddressBook addressBook : addressBooks) {
            //先把他们的默认值设置为0
            addressBook.setIsDefault(0);
            //更新数据
            addressBookDao.updateById(addressBook);
        }
        //再把当前id设置为默认值
        AddressBook addressBook = addressBookDao.selectById(id);
        addressBook.setIsDefault(1);
        //更新数据
        addressBookDao.updateById(addressBook);
    }

    @Override
    public AddressBook selectDefaultAddressBook() {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentUserId)
                .eq(AddressBook::getIsDefault, 1);
        return addressBookDao.selectOne(queryWrapper);
    }

    @Override
    public AddressBook getAddressBookInfoById(Long id) {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentUserId)
                .eq(AddressBook::getId, id);
        AddressBook addressBook = addressBookDao.selectOne(queryWrapper);
        //返回地址簿信息
        return addressBook;

    }

    @Override
    public void modifyAddressBook(AddressBook addressBook) {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,currentUserId)
                .eq(AddressBook::getId,addressBook.getId());
        AddressBook addressBook1 = addressBookDao.selectOne(queryWrapper);
        BeanUtils.copyProperties(addressBook,addressBook1);
        //更新修改时间
        addressBook1.setUpdateTime(LocalDateTime.now());
        addressBookDao.updateById(addressBook1);
    }

    @Override
    public void deleteAddressBookById(Long id) {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,currentUserId)
                .eq(AddressBook::getId,id);
        addressBookDao.delete(queryWrapper);
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public Long getCurrentUserId() {
        Long currentUserId = BaseContext.getCurrentUserId();
        return currentUserId;
    }

}
